﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace _1065093_1065130_1065120_DoAn
{
    public partial class QuanLySoLuongSanPham : UserControl
    {
        public QuanLySoLuongSanPham()
        {
            InitializeComponent();
        }

        public void LoadTim()
        {
            string tukhoa = txtsearch.Text;
            SANPHAMBUS spBus = new SANPHAMBUS();
            DataTable dt = new DataTable();
            dt = spBus.LoadDsTim(tukhoa);
            dgvsoluong.DataSource = dt;
        }

        private void btntim_Click(object sender, EventArgs e)
        {
            if (txtsearch.Text == "")
            {
                MessageBox.Show("Không có từ khoá cần tìm", "");
                return;
            }
            LoadTim();
        }

        private void dgvsoluong_SelectionChanged(object sender, EventArgs e)
        {
            lbmasp.Text = dgvsoluong.CurrentRow.Cells[0].Value.ToString();
            lbtensp.Text = dgvsoluong.CurrentRow.Cells[1].Value.ToString();
            txtghkho.Text = dgvsoluong.CurrentRow.Cells[2].Value.ToString();
            txtghquay.Text = dgvsoluong.CurrentRow.Cells[3].Value.ToString();
            txtslhttkho.Text = dgvsoluong.CurrentRow.Cells[4].Value.ToString();
            txtslhttquay.Text = dgvsoluong.CurrentRow.Cells[5].Value.ToString();
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Bạn có muốn sửa?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                try
                {
                    int ma = Convert.ToInt32(lbmasp.Text);
                    int ghkho = Convert.ToInt32(txtghkho.Text);
                    int ghquay = Convert.ToInt32(txtghquay.Text);
                    int slhttrongkho = Convert.ToInt32(txtslhttkho.Text);
                    int slhttrongquay = Convert.ToInt32(txtslhttquay.Text);

                    SANPHAMBUS spBus = new SANPHAMBUS();
                    spBus.SuaSL(ma,ghkho,ghquay,slhttrongkho,slhttrongquay);
                    LoadTim();
                    MessageBox.Show("Thành công");
                    return;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
        }

        private void txtghkho_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsNumber(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtghquay_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsNumber(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtslhttkho_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsNumber(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtslhttquay_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsNumber(e.KeyChar))
            {
                e.Handled = true;
            }
        }
    }
}
